#include <stdio.h>
#include <stdlib.h>
#include "sqstack.h"

sqstack *stack_create(void)
{
	sqstack *stack = malloc(sizeof(sqstack));
	if (stack == NULL)
		return NULL;
	stack->top = -1;
	return stack;
}

int stack_push(sqstack *st, datatype *data)
{
	if (st->top == MAXSIZE - 1)
		return -1;
	st->data[++st->top] = *data;
	return 0;
}

int stack_pop(sqstack *st, datatype *data)
{
	if (stack_isempty(st))
		return -1;
	*data = st->data[st->top--];
	return 0;
}

int stack_show(sqstack *st)
{
	if (stack_isempty(st))
		return -1;
	for (int i = 0; i <= st->top; i++)
		printf("%d	", st->data[i]);
	printf("\n");

	return 0;
}

void stack_destory(sqstack **st)
{
	free(*st);
	*st = NULL;
}

int stack_isempty(sqstack *st)
{
	return (st->top == -1);
}

int stack_top(sqstack *st, datatype *data)
{
	if (stack_isempty(st))
		return -1;
	*data = st->data[st->top];
	return 0;
}